<?php
	session_start();
	require('game_functions.php');
	$gameid = $_POST['gameid'];
	$type = $_POST['showtype'];
	$name = $type.$gameid;
	$name2 = $name.'FORM';

	dblogin();
	
	/****   Post new Values   ****/
	if($_POST['sub'] != ''){
		$game = $_POST['game'];
		$owner = $_POST['owner'];
		$des = $_POST['desc'];
		$link = $_POST['link'];
		$gametype = $_POST['gametype'];
		$num_per_team = $_POST['num_per_team'];
		$team_per_game = $_POST['team_per_game'];
		$bring1 = $_POST['bring1'];
		$bring1_num = $_POST['bring1_num'];
		$bring2 = $_POST['bring2'];
		$bring2_num = $_POST['bring2_num'];
		$bring3 = $_POST['bring3'];
		$bring3_num = $_POST['bring3_num'];
		$bring4 = $_POST['bring4'];
		$bring4_num = $_POST['bring4_num'];
		$rule1_title = $_POST['rule1_title'];
		$rule1_type = $_POST['rule1_type'];
		$rule1_optext = $_POST['rule1_optext'];
		$rule2_title = $_POST['rule2_title'];
		$rule2_type = $_POST['rule2_type'];
		$rule2_optext = $_POST['rule2_optext'];
		$rule3_title = $_POST['rule3_title'];
		$rule3_type = $_POST['rule3_type'];
		$rule3_optext = $_POST['rule3_optext'];
		$status = $_POST['status'];
		
		/*  update the db  (delete the old add the new)*/
		$querydown = 
		"delete from GAMES
		where GAMEID = ".$gameid;
		$result = mysql_query($querydown);	
		$queryup = 
		"insert into GAMES (GAMEID,GAME,OWNER,DES,LINK,TYPE,NUM_PER_TEAM,TEAM_PER_GAME,
					BRING1,BRING1_NUM,BRING2,BRING2_NUM,BRING3,BRING3_NUM,BRING4,BRING4_NUM,
					RULE1_TITLE,RULE1_TYPE,RULE1_OPTEXT,RULE2_TITLE,RULE2_TYPE,RULE2_OPTEXT,RULE3_TITLE,RULE3_TYPE,RULE3_OPTEXT, STATUS)
		  values ($gameid, '$game', $owner, '$des', '$link', '$gametype', $num_per_team, $team_per_game,
				  '$bring1', $bring1_num, '$bring2', $bring2_num, '$bring3', $bring3_num, '$bring4', $bring4_num,
				  '$rule1_title', '$rule1_type', '$rule1_optext', '$rule2_title', '$rule2_type', '$rule2_optext', '$rule3_title', '$rule3_type', '$rule3_optext',
				  '$status')
		";
		$result = mysql_query($queryup);
	}
	
	/**** Show the Values *****/
	$query = 
	"select *
	from GAMES g
	where g.gameid = ".$gameid;
	
	$result = mysql_query($query);
	if($row = mysql_fetch_array($result)){
		$game = $row['GAME'];
		$owner = $row['OWNER'];
		$des = $row['DES'];
		$link = $row['LINK'];
		$gametype = $row['TYPE'];
		$num_per_team = $row['NUM_PER_TEAM'];
		$team_per_game = $row['TEAM_PER_GAME'];
		$bring1 = $row['BRING1'];
		$bring1_num = $row['BRING1_NUM'];
		$bring2 = $row['BRING2'];
		$bring2_num = $row['BRING2_NUM'];
		$bring3 = $row['BRING3'];
		$bring3_num = $row['BRING3_NUM'];
		$bring4 = $row['BRING4'];
		$bring4_num = $row['BRING4_NUM'];
		$rule1_title = $row['RULE1_TITLE'];
		$rule1_type = $row['RULE1_TYPE'];
		$rule1_optext = $row['RULE1_OPTEXT'];
		$rule2_title = $row['RULE2_TITLE'];
		$rule2_type = $row['RULE2_TYPE'];
		$rule2_optext = $row['RULE2_OPTEXT'];
		$rule3_title = $row['RULE3_TITLE'];
		$rule3_type = $row['RULE3_TYPE'];
		$rule3_optext = $row['RULE3_OPTEXT'];
		$status = $row['STATUS'];
	
	}


?>
<script type="text/javascript">
$(document).ready(function(){
    // bind form using ajaxForm 
    $('#<?php echo $name2 ?>').ajaxForm({ 
        // target identifies the element(s) to update with the server response 
        target: '#<?php echo $name;?>', 
 
        // success identifies the function to invoke when the server response 
        // has been received; here we apply a fade-in effect to the new content 
        success: function() { 
            $('#<?php echo $name;?>').fadeIn('slow'); 
        }
    }); 
});
</script>

<div id="gamedetail<?php echo $gameid;?>" class="<?php echo $gametype; ?>" style="float:left">
	<form id="<?php echo $name2; ?>" method="post" action="Games/game_edit.php">
	<input type="hidden" name="gameid" value="<?php echo $gameid; ?>">
	<input type="hidden" name="owner" value="<?php echo $owner; ?>">
	<input type="hidden" name="showtype" value="<?php echo $type; ?>">
	<input type="hidden" name="status" value="<?php echo $status; ?>">	
	<div id="tl" class="edit">
		<div id="gen_info">
			<label>Game:</label>
			<input name="game" type="text"  value="<?php echo $game; ?>" />
			<label>Link:</label>
			<input name="link" type="text" value="<?php echo $link; ?>" />
		</div>
		<div id="pers">
			<label>Num/Team</label>
			<label>Team/Game</label>
			<input name="num_per_team"  type="text" value="<?php echo $num_per_team; ?>" />
			<input name="team_per_game" type="text" value="<?php echo $team_per_game; ?>" />
		</div>
	</div>
	<div id="description" class="edit">
		<label>Description</label>
		<textarea name="desc" rows="3"><?php echo $des; ?></textarea>
	</div>

	<div id="bringneed" class="edit">
		<h1> Items Needed per Game </h1>
		<div id="row">
			<input class="item" name="bring1"     type="text" value="<?php echo $bring1; ?>" />
			<input class="num"  name="bring1_num" type="text" value="<?php echo $bring1_num; ?>" />
		</div>
		<div id="row">
			<input class="item" name="bring2"     type="text" value="<?php echo $bring2; ?>" />
			<input class="num"  name="bring2_num" type="text" value="<?php echo $bring2_num; ?>" />
		</div>
		<div id="row">
			<input class="item" name="bring3"     type="text" value="<?php echo $bring3; ?>" />
			<input class="num"  name="bring3_num" type="text" value="<?php echo $bring3_num; ?>" />
		</div>
		<div id="row">
			<input class="item" name="bring4"     type="text" value="<?php echo $bring4; ?>" />
			<input class="num"  name="bring4_num" type="text" value="<?php echo $bring4_num; ?>" />
		</div>
	</div>

	<div id="blankback"  class="edit">
		<iframe  class="preview" width="100%" height="130px" src="<?php echo $link;?>"></iframe>
	</div>

	<div id="mr" class="edit">
		<label>Type </label><br />
		<input type="radio" name="gametype" value="VG" <?php if($gametype == 'VG'){ echo 'checked';} ?> /> Video Game <br />
		<input type="radio" name="gametype" value="CG"  <?php if($gametype == 'CG'){ echo 'checked';} ?> /> Card Game <br />
		<input type="radio" name="gametype" value="BG" <?php if($gametype == 'BG'){ echo 'checked';} ?> /> Board Game <br />
		<input type="radio" name="gametype" value="OT" <?php if($gametype == 'OT'){ echo 'checked';} ?> /> Other <br />
	</div>	


	<div id="rule" class="edit">
		<input class="title"  name="rule1_title"  type="text" value="<?php echo $rule1_title; ?>" />
		<input type="radio" name="rule1_type" value="RR" <?php if($rule1_type == 'RR'){ echo 'checked = "yes"';} ?> /> Round Robin 
		<input type="radio" name="rule1_type" value="SE" <?php if($rule1_type == 'SE'){ echo 'checked = "yes"';} ?> /> Single Elim <br />
		<input type="radio" name="rule1_type" value="DE" <?php if($rule1_type == 'DE'){ echo 'checked = "yes"';} ?> /> Double Elim 
		<input type="radio" name="rule1_type" value="OT" <?php if($rule1_type == 'OT'){ echo 'checked = "yes"';} ?> /> Other <br />	
		<textarea class="detail" name="rule1_optext" > <?php echo $rule1_optext; ?></textarea>
	</div>
	<div id="rule" class="edit">
		<input class="title"  name="rule2_title"  type="text" value="<?php echo $rule2_title; ?>" />
		<input type="radio" name="rule2_type" value="RR" <?php if($rule2_type == 'RR'){ echo 'checked = "yes"';} ?> /> Round Robin 
		<input type="radio" name="rule2_type" value="SE" <?php if($rule2_type == 'SE'){ echo 'checked = "yes"';} ?> /> Single Elim <br />
		<input type="radio" name="rule2_type" value="DE" <?php if($rule2_type == 'DE'){ echo 'checked = "yes"';} ?> /> Double Elim 	
		<input type="radio" name="rule2_type" value="OT" <?php if($rule2_type == 'OT'){ echo 'checked = "yes"';} ?> /> Other <br />	
		<textarea class="detail" name="rule2_optext" > <?php echo $rule2_optext; ?></textarea>
	</div>
	<div id="rule" class="edit">
		<input class="title"  name="rule3_title"  type="text" value="<?php echo $rule3_title; ?>" />
		<input type="radio" name="rule3_type" value="RR" <?php if($rule3_type == 'RR'){ echo 'checked = "yes"';} ?> /> Round Robin 
		<input type="radio" name="rule3_type" value="SE" <?php if($rule3_type == 'SE'){ echo 'checked = "yes"';} ?> /> Single Elim <br />
		<input type="radio" name="rule3_type" value="DE" <?php if($rule3_type == 'DE'){ echo 'checked = "yes"';} ?> /> Double Elim 
		<input type="radio" name="rule3_type" value="OT" <?php if($rule3_type == 'OT'){ echo 'checked = "yes"';} ?> /> Other  <br />	
		<textarea class="detail" name="rule3_optext" > <?php echo $rule3_optext; ?></textarea>
	</div>

	<div id="buttons" class="edit">
		<input type="submit" value="submit" name="sub" />
		<input type="button" value="cancel" id="cancel" name="cancel"/>
	</div>
	</form>
</div>